1 using System.Collections.Generic;
2
3 namespace ProceduralToolkit.Examples
4 {
5 /// <summary>
6 /// Cellular automaton ruleset representation
7 /// </summary>
8 public struct Ruleset
9 {
10 #region Common rulesets
11
12 public static Ruleset life { get { return new Ruleset("3", "23"); } }
13 public static Ruleset highlife { get { return new Ruleset("36", "23"); } }
14 public static Ruleset lifeWithoutDeath { get { return new Ruleset("3", "012345678"); } }
15 public static Ruleset thirtyFour { get { return new Ruleset("34", "34"); } }
16 public static Ruleset inverseLife { get { return new Ruleset("0123478", "34678"); } }
17 public static Ruleset pseudoLife { get { return new Ruleset("357", "238"); } }
18 public static Ruleset longLife { get { return new Ruleset("345", "5"); } }
19 public static Ruleset dotLife { get { return new Ruleset("3", "023"); } }
20 public static Ruleset dryLife { get { return new Ruleset("37", "23"); } }
21 public static Ruleset seeds { get { return new Ruleset("2"); } }
22 public static Ruleset serviettes { get { return new Ruleset("234"); } }
23 public static Ruleset gnarl { get { return new Ruleset("1", "1"); } }
24 public static Ruleset liveFreeOrDie { get { return new Ruleset("2", "0"); } }
25 public static Ruleset dayAndNight { get { return new Ruleset("3678", "34678"); } }
26 public static Ruleset replicator { get { return new Ruleset("1357", "1357"); } }
27 public static Ruleset twoXTwo { get { return new Ruleset("36", "125"); } }
28 public static Ruleset move { get { return new Ruleset("368", "245"); } }
29 public static Ruleset maze { get { return new Ruleset("3", "12345"); } }
30 public static Ruleset mazectric { get { return new Ruleset("3", "1234"); } }
31 public static Ruleset amoeba { get { return new Ruleset("357", "1358"); } }
32 public static Ruleset diamoeba { get { return new Ruleset("35678", "5678"); } }
33 public static Ruleset coral { get { return new Ruleset("3", "45678"); } }
34 public static Ruleset anneal { get { return new Ruleset("4678", "35678"); } }
35 public static Ruleset majority { get { return new Ruleset("5678", "45678"); } }
36 public static Ruleset walledCities { get { return new Ruleset("45678", "2345"); } }
37 public static Ruleset stains { get { return new Ruleset("3678", "235678"); } }
38 public static Ruleset coagulations { get { return new Ruleset("378", "235678"); } }
39 public static Ruleset assimilation { get { return new Ruleset("345", "4567"); } }
40
41 #endregion Common rulesets
42
43 private readonly List<int> birthRule;
44 private readonly List<int> survivalRule;
45
46 public Ruleset(string birthRule = null, string survivalRule = null)
47 {
48 this.birthRule = ConvertRuleStringToList(birthRule);
49 this.survivalRule = ConvertRuleStringToList(survivalRule);
50 }
51
52 public bool CanSpawn(int aliveCells)
53 {
54 return birthRule.Contains(aliveCells);
55 }
56
57 public bool CanSurvive(int aliveCells)
58 {
59 return survivalRule.Contains(aliveCells);
60 }
61
62 private static List<int> ConvertRuleStringToList(string rule)
63 {
64 var list = new List<int>();
65 if (!string.IsNullOrEmpty(rule))
66 {
67 foreach (char c in rule)
68 {
69 if (char.IsDigit(c))
70 {
71 list.Add((int) char.GetNumericValue(c));
72 }
73 }
74 list.Sort();
75 }
76 return list;
77 }
78
79 public override string ToString()
80 {
81 string b = "";
82 foreach (var digit in birthRule)
83 {
84 b += digit;
85 }
86 string s = "";
87 foreach (var digit in survivalRule)
88 {
89 s += digit;
90 }
91 return string.Format("B{0}/S{1}", b, s);
92 }
93 }
94 }